﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using SharedGenomics.Annotations;
using SharedGenomics.Workbench.Annotations;
using SharedGenomics.Workbench.Tests.TestObjects;

using Rhino.Mocks;
using Rhino.Mocks.Constraints;

namespace SharedGenomics.Workbench.Tests.Annotations
{
    [TestClass]
    public class NotificationAnnotationRequestStageTests
    {
        private MockRepository mocks;
        private IAnnotationRequestStatusNotifier notifier;
        private NotificationAnnotationRequestStage stage;

        [TestInitialize]
        public void Setup()
        {
            this.mocks = new MockRepository();
            this.notifier = this.mocks.StrictMock<IAnnotationRequestStatusNotifier>();
            this.stage = new NotificationAnnotationRequestStage(this.notifier);
        }

        [TestMethod]
        public void ShouldNotifyRequestSucceeded()
        {
            var annotation = new GoogleSearchAnnotation("rs001");
            using (this.mocks.Record())
            {
                AnnotationRequestResult expectedResult = new SucceededAnnotationRequestResult(AnnotationKey.GoogleSearch, "rs001", annotation, null);
                this.notifier.Expect(n => n.RequestCompleted(null)).IgnoreArguments().Constraints(Property.AllPropertiesMatch(expectedResult));
            }

            using (this.mocks.Playback())
            {
                var context = new AnnotationRequestContext(AnnotationKey.GoogleSearch, "rs001");
                context.AddAnnotation(annotation, null);
                var traversalStrategy = new TestAnnotationRequestPipelineTraversalStrategy();

                this.stage.Process(context, traversalStrategy);
            }
        }

        [TestMethod]
        public void ShouldNotifyRequestFailed()
        {
            using (this.mocks.Record())
            {
                AnnotationRequestResult expectedResult = new FailedAnnotationRequestResult(AnnotationKey.GoogleSearch, "rs001");
                this.notifier.Expect(n => n.RequestCompleted(null)).IgnoreArguments().Constraints(Property.AllPropertiesMatch(expectedResult));
            }

            using (this.mocks.Playback())
            {
                var context = new AnnotationRequestContext(AnnotationKey.GoogleSearch, "rs001");
                context.FailRequest(AnnotationKey.GoogleSearch);
                var traversalStrategy = new TestAnnotationRequestPipelineTraversalStrategy();

                this.stage.Process(context, traversalStrategy);
            }
        }
    }
}
